
package pmbs;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;


/**
 *
 * @author Robert Yonas
 */
public class Utility {
    /**
     * This class will handle config file management and log file management
     */
    public static void initConfig() {
        Properties myProp = new Properties();

        try{
            // try to load properties file
            myProp.load(new FileInputStream("CareerDay.Config"));
        } catch (IOException e) {
                Logger.getLogger("pmbs.CareerDay").log(java.util.logging.Level.SEVERE, null, e);
            try {
                //set config default value
                myProp.setProperty("DatabaseName", "CareerDay");
                myProp.setProperty("UserName", "postgres");
                myProp.setProperty("Password", "pmbscd");
                myProp.setProperty("Server", "localhost");
                myProp.setProperty("Port", "5432");
                myProp.setProperty("RefreshTime","1");
                
                myProp.store(new FileOutputStream("CareerDay.Config"), null);
            } catch (IOException ex) {  
                Logger.getLogger("pmbs.CareerDay").log(java.util.logging.Level.SEVERE, null, ex);
            }
        }
    }
    
    public static String[] getConfig() {
        //this method will return config value
        Properties myProp = new Properties();
        String[] result = new String[5];
         try{
            // try to load properties file
            myProp.load(new FileInputStream("CareerDay.Config"));
            
            //load all value
            result[0] = myProp.getProperty("DatabaseName");
            result[1] = myProp.getProperty("UserName");
            result[2] = myProp.getProperty("Password");
            result[3] = myProp.getProperty("Server");
            result[4] = myProp.getProperty("Port");
            
        } catch (IOException ex) {
                Logger.getLogger("pmbs.CareerDay").log(java.util.logging.Level.SEVERE, null, ex);
        }
         
         return result;
    }
    
    public static void saveConfig(String DatabaseName, String UserName, String Password, String Server, String Port) {
         Properties myProp = new Properties();
            try {
                //set config default value
                myProp.setProperty("DatabaseName", DatabaseName);
                myProp.setProperty("UserName", UserName);
                myProp.setProperty("Password", Password);
                myProp.setProperty("Server", Server);
                myProp.setProperty("Port", Port);
                myProp.setProperty("RefreshTime","1");
                
                myProp.store(new FileOutputStream("CareerDay.Config"), null);
            } catch (IOException ex) {  
                Logger.getLogger("pmbs.CareerDay").log(java.util.logging.Level.SEVERE, null, ex);
            }
    }
    
    public static int getTimeInterval() {
        //this method will return config value
        Properties myProp = new Properties();
        int result=1;
         try{
            // try to load properties file
            myProp.load(new FileInputStream("CareerDay.Config"));
            
            //load all value
            result = Integer.parseInt(myProp.getProperty("RefreshTime"));
            
        } catch (IOException ex) {
                Logger.getLogger("pmbs.CareerDay").log(java.util.logging.Level.SEVERE, null, ex);
        }
         
         return result;        
    }
    
    /**
     * This part is handling log file
     */
    
    public static void initLog() {
        FileHandler handler = null;
        try {
            handler = new FileHandler("CareerDay.Log", 50*1024*1024, 1, true);
        } catch (IOException e) {
            try {
                handler = new FileHandler("CareerDay.Log", 50*1024*1024, 1);
            } catch (IOException ex){
                Logger.getLogger("pmbs.CareerDay").log(java.util.logging.Level.SEVERE, null, ex);
            }
            
        }
        
        handler.setFormatter(new SimpleFormatter());
        Logger myLog = Logger.getLogger("pmbs.CareerDay");
        myLog.addHandler(handler);
    }
}
